﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace ProductMonitor.Command
{
    public class RelayCommand : ICommand
    {
        public Action _action { get; set; }
        public event EventHandler? CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }//会自动触发
        }

        public RelayCommand(Action objectAction)
        {
            this._action = objectAction;
        }

        public bool CanExecute(object? parameter)
        {
            return _action != null;
        }

        public void Execute(object? parameter)
        {
            if (_action != null)
            {
                _action();
            }
        }  
    }
}
